package LogisticsSystem.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import LogisticsSystem.po.Dispatch;
import LogisticsSystem.po.Orders;
import LogisticsSystem.po.Staff;
import LogisticsSystem.po.ext.DispatchExtend;
import LogisticsSystem.service.DispatcherService;
import LogisticsSystem.service.OrderService;
import LogisticsSystem.service.StaffService;

/**
 * @author 作者 slim
 * @version 创建时间：2017年11月23日 下午4:27:02 类说明
 */
@Controller
@RequestMapping("dispatcher")
public class DispatcherController {

	@Autowired
	DispatcherService dispatcherService;
	@Autowired
	StaffService staffService;
	@Autowired
	OrderService orderService;

	@RequestMapping("/selectDispatchsBySId")
	public ModelAndView selectDispatchsBySId(HttpServletRequest request) {
		String role = ((Staff)request.getSession().getAttribute("staff")).getsRole();
		int sId = ((Staff)request.getSession().getAttribute("staff")).getsId();
		ModelAndView modelAndView = new ModelAndView();
		List<DispatchExtend> DEList = dispatcherService.getDispatchsBySId(sId);
		modelAndView.addObject("DEList", DEList);
		List<Staff> dispatchers = staffService.selectAllDispatchByRole2("3");
		modelAndView.addObject("dispatchers", dispatchers);
		if(role.equals("2")) {
			modelAndView.setViewName("dispatcher/changDispatchsStatu");
		}else if (role.equals("3")) {
			modelAndView.setViewName("dispatcher/toDispatch");
		}
		return modelAndView;
	}

	@RequestMapping("/getDispatcher")
	public ModelAndView getDispatcher() {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("dispatcher/toDispatch");
		List<DispatchExtend> DEList = dispatcherService.getAllDispatchs();
		modelAndView.addObject("DEList", DEList);
		return modelAndView;
	}

	@RequestMapping("changeDispatchStatus")
	public ModelAndView changeDispatchStatus(HttpServletRequest request) {
		String path = request.getRequestURI();
		System.out.println(path);
		ModelAndView modelAndView = new ModelAndView();
		int oid = Integer.valueOf(request.getParameter("dispatch_3"));
		int status = Integer.valueOf(request.getParameter("dispatch_5"));
		Orders order = new Orders();
		order.setoId(oid);
		order.setoStatus(status);
		orderService.updateOrders(order);
		/*
		 * int sid =((Staff)request.getSession().getAttribute("staff")).getsId();
		 * List<DispatchExtend> dispatchsList =
		 * dispatcherService.getDispatchsBySId(sid);
		 */
		List<DispatchExtend> DEList = dispatcherService.getAllDispatchs();
		modelAndView.addObject("DEList", DEList);
		modelAndView.setViewName("dispatcher/toDispatch");
		return modelAndView;
	}

	@RequestMapping("changeDispatchStatu")
	public ModelAndView changeDispatchStatu(HttpServletRequest request) {
		ModelAndView modelAndView = new ModelAndView();
		int oid = Integer.valueOf(request.getParameter("dispatch_3"));
		int status = Integer.valueOf(request.getParameter("dispatch_5"));
		int s_id = Integer.valueOf(request.getParameter("dispatch_6"));
		Orders order = new Orders();
		order.setoId(oid);
		order.setoStatus(status);
		orderService.updateOrders(order);
		
		Dispatch dispatch = dispatcherService.getDispatchByOId(oid);
		dispatch.setsId(s_id);
		dispatcherService.updateDispatchs(dispatch);

		int sid = ((Staff) request.getSession().getAttribute("staff")).getsId();
		List<DispatchExtend> DEList = dispatcherService.getDispatchsBySId(sid);
		List<Staff> dispatchers = staffService.selectAllDispatchByRole2("3");
		modelAndView.addObject("dispatchers", dispatchers);
		modelAndView.addObject("DEList", DEList);
		modelAndView.setViewName("dispatcher/changDispatchsStatu");
		return modelAndView;
	}

}
